<?php
/**
 * Created by JetBrains PhpStorm.
 * User: Spinatiev Ivan
 * Date: 11/6/12
 * Time: 11:54 AM
 * To change this template use File | Settings | File Templates.
 */

class Db_Products_Prices extends Common {
	protected static $tableName = TAB_PRICES;
	protected static $dbFields = array(
		'id',
		'pc_project_id',
		'pc_category_id',
		'pc_size_id',
		'pc_amount_id',
		'pc_price',
		'deleted'
	);

	/**
	 * @var Db_Products_Prices
	 */
	public $objectArray;

	public $id;
	public $pc_project_id;
	public $pc_category_id;
	public $pc_size_id;
	public $pc_amount_id;
	public $pc_price;
	public $deleted;

	function __construct($value=NULL, $fieldName=NULL) {
		parent::__construct($value, $fieldName);
	}

	/**
	 * @static
	 * @param $projectId
	 * @return array|bool
	 */
	public static function getPricesByProjectId($projectId) {
		if (!$projectId) {
			return false;
		}
		$sql = "SELECT * FROM ?f WHERE `pc_project_id` = ? AND deleted = 0";
		$prices = self::$DB->q(	$sql, self::$tableName, $projectId);
		$dataReturn = array();

		while($data = $prices->fetch_object()) {
			$dataReturn[$data->pc_category_id][$data->pc_size_id][$data->pc_amount_id] = $data->pc_price;
		}
		return $dataReturn;
	}

	/**
	 * @static
	 * @param $categoryId
	 * @param $sizeId
	 * @return bool|mixed
	 */
	public static function getPricesInfo($categoryId, $sizeId) {
		if (!$categoryId || !$sizeId) {
			return false;
		}
		$sql = "SELECT pc.id, pc.pc_price, am.am_amount, am.am_is_default, am.am_is_sample FROM ?f pc
				INNER JOIN ?f am ON pc.pc_amount_id = am.id AND am.deleted = 0
				WHERE pc.pc_project_id = ?d AND pc.pc_category_id = ?d AND pc.pc_size_id = ?d AND pc.deleted = 0
				ORDER BY am.am_amount ASC ";
		return self::$DB->select($sql, self::$tableName, TAB_AMOUNTS, self::$PROJECT['id'], $categoryId, $sizeId);
	}

	public static function getPricesForStep($sessionProductsList) {
		$prices = array();
		if ($sessionProductsList) {
			foreach ($sessionProductsList as $key => $sessionProduct) {
				if (!isset($sessionProduct['productId']) || !isset($sessionProduct['categoryId'])) {
					return false;
				}
				$key  = Utils::memoryProductIdDecode($key);
				$categoryId =  $sessionProduct['categoryId'];
				$productSize = Db_Products_Products::findArrayById(Utils::memoryProductIdDecode($sessionProduct['productId']));

				$sql = "SELECT `pc`.*, `am`.`am_amount`, `am`.`am_is_default`, `am`.`am_is_sample`  FROM ?f pc
                        INNER JOIN ?f am ON `am`.`id` = `pc`.`pc_amount_id` AND `am`.`deleted` = 0
		                WHERE `pc`.`deleted` = 0  AND `pc`.`pc_project_id` = ? AND `pc`.`pc_category_id` = ? AND `pc`.`pc_size_id` = ? ";
				$prices[$key] = self::$DB->select($sql, self::$tableName, TAB_AMOUNTS, self::$PROJECT['id'], $categoryId, $productSize['p_size_id']);
			}
		} else {
			return false;
		}
	return $prices;
	}


	/**
	 * @static
	 * @param $priceId
	 * @param $projectId
	 * @param $categoryId
	 * @param $sizeId
	 * @return mixed
	 */
	public static function getAmountForPrice($priceId, $projectId, $categoryId, $sizeId) {
		$sql = "SELECT pc.id as priceId,am.am_amount FROM ?f pc
					INNER JOIN ?f am ON am.id = pc.pc_amount_id
					WHERE pc.pc_project_id = ?d AND pc.pc_category_id = ?d AND pc.pc_size_id = ?d
					AND (pc.id = ?d OR am.am_is_default = 1) AND am.deleted = 0	AND pc.deleted = 0
					ORDER BY am.am_is_default ASC
					LIMIT 1
				";
		return self::$DB->selectRow($sql, self::$tableName, TAB_AMOUNTS, $projectId, $categoryId, $sizeId, $priceId);

	}

}